package org.lg.service.impl;

import org.lg.config.Resource;
import org.lg.config.Service;
import org.lg.core.InitMapping;
import org.lg.dao.AccoutDao;
import org.lg.dao.StudentDao;
import org.lg.dao.TeacherDao;
import org.lg.dto.BaseDto;
import org.lg.dto.MessageEnum;
import org.lg.service.LoginService;
import org.lg.utils.CommandUtils;
import org.lg.vo.Account;

import java.util.Optional;

@Service("loginService")
public class LoginServiceImpl implements LoginService {
    @Resource
    StudentDao studentDao;
    @Resource
    TeacherDao teacherDao;
    @Override
    public BaseDto login(Account accout) throws Exception {
        String role = accout.getRole();
        String username = accout.getUsername();
        String password = accout.getPassword();
        String msg = "";
        MessageEnum messageEnum;
        System.out.println(role);
        AccoutDao baseDao = (AccoutDao) InitMapping.daoMaps.get(CommandUtils.changeFirstCharLower(role)+"Dao");

                Account account = baseDao.getAccountByUsername(username);
                Optional<Account> op = Optional.ofNullable(account);
        System.out.println("password:"+password+" " + account);
                messageEnum =  op.isPresent()?(account.getPassword().equals(password)?MessageEnum.LOGINSUCCESS:MessageEnum.LOGINPASSWORDFAIL)
                        :MessageEnum.LOGINUSERNAMEFAIL;

        BaseDto dto = new BaseDto(messageEnum,account);
        return dto;
    }
}
